<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="卡号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入卡号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者编号" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入患者编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员等级" prop="vipGrade">
        <el-select v-model="queryParams.vipGrade" placeholder="请选择会员等级" clearable>
          <el-option
            v-for="dict in doctor_vip"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['jy:vipstorageManagement:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jy:vipstorageManagement:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jy:vipstorageManagement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['jy:vipstorageManagement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vipstorageManagementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="卡号" align="center" prop="id" />
      <el-table-column label="会员等级" align="center" prop="vipGrade">
        <template #default="scope">
          <dict-tag :options="doctor_vip" :value="scope.row.vipGrade"/>
        </template>
      </el-table-column>
      <el-table-column label="会员姓名" align="center" prop="doctorPatient.name" />
      <el-table-column label="手机号码" align="center" prop="doctorPatient.phone" />
      <el-table-column label="累计储值" align="center" prop="deposit" />
      <el-table-column label="储值余额" align="center" prop="balance" />
      <el-table-column label="累计消费" align="center" prop="consumption" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Money" @click="handleUpdate(scope.row)" v-hasPermi="['jy:vipstorageManagement:edit']">充值</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['jy:vipstorageManagement:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员储值管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="vipstorageManagementRef" :model="form" :rules="rules" label-width="80px">
        <!-- 显示会员姓名 -->
        <el-form-item label="会员姓名">
          <el-tag>{{ form.name }}</el-tag>
        </el-form-item>
        <!-- 显示手机号码 -->
        <el-form-item label="手机号码">
          <el-tag>{{ form.phone }}</el-tag>
        </el-form-item>
        <!-- 显示累计储值 -->
        <el-form-item label="累计储值" prop="deposit">
          <el-tag>{{ form.deposit }}</el-tag>
        </el-form-item>
        <!-- 显示储值余额 -->
        <el-form-item label="储值余额">
          <el-tag>{{ form.balance }}</el-tag>
        </el-form-item>
        <!-- 充值金额输入框 -->
        <el-form-item label="充值金额" prop="rechargeAmount">
          <el-input v-model="form.rechargeAmount" placeholder="请输入充值金额" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="VipstorageManagement">
import { listVipstorageManagement, getVipstorageManagement, delVipstorageManagement, addVipstorageManagement, updateVipstorageManagement } from "@/api/jy/vipstorageManagement";

const { proxy } = getCurrentInstance();
const { doctor_vip } = proxy.useDict('doctor_vip');

const vipstorageManagementList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    patientId: null,
    vipGrade: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询会员储值管理列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  listVipstorageManagement(queryParams.value).then(response => {
    vipstorageManagementList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    patientId: null,
    vipGrade: null,
    consumption: null,
    balance: null,
    deposit: null,
    points: null,
    createTime: null,
    deadlineTime: null,
    del: null
  };
  proxy.resetForm("vipstorageManagementRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加会员储值管理";
}

/** 充值按钮按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = {
    id: row.id, // 会员储值管理的ID
    patientId: row.patientId, // 患者编号
    vipGrade: row.vipGrade, // 会员等级
    name: row.doctorPatient.name, // 会员姓名
    phone: row.doctorPatient.phone, // 手机号码
    balance: row.balance, // 储值余额
    deposit: row.deposit // 累计储值
  };
  open.value = true;
  title.value = "充值操作";
}

/** 提交充值金额 */
function submitForm() {
  proxy.$refs["vipstorageManagementRef"].validate(valid => {
    if (valid) {
      // 获取用户输入的充值金额
      const rechargeAmount = parseFloat(form.value.rechargeAmount || 0);
      // 检查充值金额是否有效
      if (isNaN(rechargeAmount) || rechargeAmount <= 0) {
        proxy.$modal.msgError("请输入有效的充值金额");
        return;
      }
      // 调用更新接口，传递充值金额
      const updateData = {
        id: form.value.id,
        deposit: rechargeAmount, // 充值金额
        balance: rechargeAmount // 充值金额
      };
      updateVipstorageManagement(updateData).then(response => {
        proxy.$modal.msgSuccess("充值成功");
        open.value = false;
        getList(); // 刷新列表
      }).catch(error => {
        proxy.$modal.msgError("充值失败：" + error.message);
      });
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除会员储值管理编号为"' + _ids + '"的数据项？').then(function() {
    return delVipstorageManagement(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('jy/vipstorageManagement/export', {
    ...queryParams.value
  }, `vipstorageManagement_${new Date().getTime()}.xlsx`)
}

getList();
</script>
